package pers.hl.lib_global.model.bean

import android.os.Parcel
import android.os.Parcelable

/**
{
"userId": 1,
"userName": "测试用户",
"userGender": 1,
"useAge": 18,
"userAddr": "四川成都",
"userCredit": 100,
"userRole": 1,
"userTel": "11111111111",
"account": "test",
"token": "eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MTEyNjk0NjUsInVzZXIiOnsidXNlcklkIjoxLCJ1c2VyTmFtZSI6Iua1i-ivleeUqOaItyIsInVzZXJHZW5kZXIiOjEsInVzZUFnZSI6MTgsInVzZXJBZGRyIjoi5Zub5bed5oiQ6YO9IiwidXNlckNyZWRpdCI6MTAwLCJ1c2VyUm9sZSI6MSwidXNlclRlbCI6IjExMTExMTExMTExIiwiYWNjb3VudCI6InRlc3QiLCJ0b2tlbiI6bnVsbCwiYWRtaW4iOmZhbHNlfSwiaWF0IjoxNjA5MTIzODY1LCJqdGkiOiI1YWUzNzUyNC04MDE5LTRmN2UtOTIxMy03MDFkMTcwMzE5NTEifQ.lOH7PQ5k7adDYYvPmspbOlZA3LU0PJ1o-8vHoUa_jsk",
"admin": false
}
 */
data class User(
    var account: String?,
    var admin: Boolean,
    var token: String?,
    var useAge: Int,
    var userAddr: String?,
    var userCredit: Int,
    var userGender: Int,
    var userId: Int,
    var userName: String?,
    var userRole: Int,
    var userTel: String?,
    var password: String?,
    var sharingCount: String?,
    var followCount: String?,
    var fansCount: String?
) : Parcelable {

    fun isAdmin(): Boolean {
        return userRole == ROLE_ADMIN || admin
    }

    fun getUserGenderStr(): String {
        return if (userGender == 1) "男" else if (userGender == 2) "女" else "未知"
    }

    constructor(source: Parcel) : this(
        source.readString(),
        1 == source.readInt(),
        source.readString(),
        source.readInt(),
        source.readString(),
        source.readInt(),
        source.readInt(),
        source.readInt(),
        source.readString(),
        source.readInt(),
        source.readString(),
        source.readString(),
        source.readString(),
        source.readString(),
        source.readString()
    )

    override fun describeContents() = 0

    override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
        writeString(account)
        writeInt((if (admin) 1 else 0))
        writeString(token)
        writeInt(useAge)
        writeString(userAddr)
        writeInt(userCredit)
        writeInt(userGender)
        writeInt(userId)
        writeString(userName)
        writeInt(userRole)
        writeString(userTel)
        writeString(password)
        writeString(sharingCount)
        writeString(followCount)
        writeString(fansCount)
    }

    companion object {
        @JvmField
        val CREATOR: Parcelable.Creator<User> = object : Parcelable.Creator<User> {
            override fun createFromParcel(source: Parcel): User = User(source)
            override fun newArray(size: Int): Array<User?> = arrayOfNulls(size)
        }

        // 普通用户
        const val ROLE_COMMON = 1

        // 管理员
        const val ROLE_ADMIN = 2
    }

    fun userRoleStr(): String {
        return if (isAdmin()) "管理员" else "普通用户"
    }
}